package com.qmerak.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qmerak.common.SysConstants;
import com.qmerak.common.ViewUtil;
import com.qmerak.entity.MemberEntity;
import com.qmerak.entity.OrgEntity;
import com.qmerak.entity.PostUnitEntity;
import com.qmerak.service.IMemberService;
import com.qmerak.service.IOrgService;
import com.qmerak.service.IPostUnitService;

/**
 * @author Shaohong•Cheng
 * @mail shaohong726@126.com
 * @date 2013-4-15
 * @project WebTest
 */
@Controller
@RequestMapping("/view")
public class ViewAction {
	@Autowired
	private IOrgService orgService;
	@Autowired
	private IMemberService memberService;
	@Autowired
	private IPostUnitService unitService;

	@ResponseBody
	@RequestMapping(value = "tree", method = RequestMethod.GET)
	public List<OrgEntity> getOrgnizations(HttpServletRequest request) {
		MemberEntity memberEntity = (MemberEntity) request.getSession()
				.getAttribute(SysConstants.SYS_LOGIN_USER);
		List<OrgEntity> list = new ArrayList<OrgEntity>();

		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(memberEntity
				.getLoginName())) {
			list = orgService.list();
		} else if (ViewUtil.isZhuXi(memberEntity)
				|| ViewUtil.isFuZhuXi(memberEntity)
				|| ViewUtil.isGanBu(memberEntity)) {
			list = orgService.listByManager(memberEntity);
		}
		for (OrgEntity orgEntity : list) {
			orgEntity.setMembers(memberService.listOfOrg(orgEntity));
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "orgcombo")
	public List<Map<String, Object>> getOrgComboData() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Map<String, Object>> tempMap = new HashMap<String, Map<String, Object>>();
		List<OrgEntity> entities = orgService.list();
		for (OrgEntity orgEntity : entities) {
			Map<String, Object> itemMap = new HashMap<String, Object>();
			itemMap.put("id", orgEntity.getId());
			itemMap.put("text", orgEntity.getUnionName());
			itemMap.put("parentid", orgEntity.getParentid());
			tempMap.put(orgEntity.getId(), itemMap);
		}
		for (Map.Entry<String, Map<String, Object>> entry : tempMap.entrySet()) {
			Map<String, Object> map = entry.getValue();
			Object parentid = map.get("parentid");
			if (null == parentid || "".equals(parentid)
					|| "null".equals(parentid)) {
				result.add(map);
			} else {
				Map<String, Object> pMap = tempMap.get(parentid);
				pMap.put("state", "closed");
				List<Map<String, Object>> children = (List<Map<String, Object>>) pMap
						.get("children");
				if (children == null) {
					children = new ArrayList<Map<String, Object>>();
					pMap.put("children", children);
				}
				children.add(map);
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "unitcombo")
	public List<Map<String, Object>> getUnitComboData() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		Map<String, Map<String, Object>> tempMap = new HashMap<String, Map<String, Object>>();
		List<PostUnitEntity> entities = unitService.list();
		for (PostUnitEntity unitEntity : entities) {
			Map<String, Object> itemMap = new HashMap<String, Object>();
			itemMap.put("id", unitEntity.getId());
			itemMap.put("text", unitEntity.getUnitName());
			itemMap.put("parentid", unitEntity.getParentid());
			tempMap.put(unitEntity.getId(), itemMap);
		}
		for (Map.Entry<String, Map<String, Object>> entry : tempMap.entrySet()) {
			Map<String, Object> map = entry.getValue();
			Object parentid = map.get("parentid");
			if (null == parentid || "".equals(parentid)) {
				result.add(map);
			} else {
				Map<String, Object> pMap = tempMap.get(map.get("parentid"));
				pMap.put("state", "closed");
				List<Map<String, Object>> children = (List<Map<String, Object>>) pMap
						.get("children");
				if (children == null) {
					children = new ArrayList<Map<String, Object>>();
					pMap.put("children", children);
				}
				children.add(map);
			}
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "findMembers")
	public List<MemberEntity> findMembers(HttpServletRequest request) {
		String condition = request.getParameter("condition");
		MemberEntity memberEntity = (MemberEntity) request.getSession()
				.getAttribute(SysConstants.SYS_LOGIN_USER);
		List<OrgEntity> list = new ArrayList<OrgEntity>();
		if (SysConstants.SYS_MANAGER_LOGINNAME.equals(memberEntity
				.getLoginName())) {
			list = orgService.list();
		} else if (ViewUtil.isZhuXi(memberEntity)
				|| ViewUtil.isFuZhuXi(memberEntity)
				|| ViewUtil.isGanBu(memberEntity)) {
			list = orgService.listByManager(memberEntity);
		}
		List<MemberEntity> result = new ArrayList<MemberEntity>();
		for (OrgEntity orgEntity : list) {
			result.addAll(memberService.listOfOrg(orgEntity, condition));
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "findUnits")
	public List<PostUnitEntity> findUnits(HttpServletRequest request) {
		String condition = request.getParameter("condition");
		return unitService.queryByCondition(condition);
	}

	@ResponseBody
	@RequestMapping(value = "findUnions")
	public List<OrgEntity> findUnions(HttpServletRequest request) {
		String condition = request.getParameter("condition");
		return orgService.findByCondition(condition);
	}

	@ResponseBody
	@RequestMapping(value = "loginmember")
	public MemberEntity getLoginMember(HttpServletRequest request) {
		return (MemberEntity) request.getSession().getAttribute(
				SysConstants.SYS_LOGIN_USER);
	}

}
